package com.zwl.sjzmanagement.controller;

import com.atguigu.tingshu.common.result.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwl.model.ExpansionBox;
import com.zwl.sjzmanagement.service.ExpansionBoxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/expansionBox")
public class ExpansionBoxController {

    @Autowired
    private ExpansionBoxService expansionBoxService;


    //http://localhost:8002/api/expansionBox/expansionBoxList/{pageNo}/{pageNum}
    @GetMapping("/expansionBoxList/{pn}/{pz}")
    public Result expansionBoxList(@PathVariable Long pn,@PathVariable Long pz){

        IPage<ExpansionBox> page = new Page<>(pn, pz);
        List<ExpansionBox> expansionBoxList = expansionBoxService.expansionBoxList(page);
        page.setRecords(expansionBoxList);

        return Result.ok(page);
    }

    @GetMapping("/getExpansionBoxById/{id}")
    public Result getExpansionBoxById(@PathVariable Long id) {

        ExpansionBox expansionBox = expansionBoxService.getById(id);
        return Result.ok(expansionBox);

    }

//    http://localhost:8001/api/expansionBox/expansionBoxAddOrUpdate
    @PostMapping("/expansionBoxAddOrUpdate")
    public Result expansionBoxAddOrUpdate(@RequestBody ExpansionBox expansionBox){

        expansionBoxService.saveOrUpdate(expansionBox);

        return Result.ok();
    }

//    http://localhost:8001/api/expansionBox/expansionBoxDelById

    @DeleteMapping("/expansionBoxDelById/{id}")
    public Result expansionBoxDelById(@PathVariable Integer id) {

        expansionBoxService.removeById(id);

        return Result.ok();

    }

    @PostMapping("/expansionBoxBatchDelById")
    public Result expansionBoxBatchDelById(@RequestBody List<Integer> ids) {

        //根据id判断，相同则执行跟新操作
        expansionBoxService.removeBatchByIds(ids);

        return Result.ok();

    }


}
